﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_BOM_MASTER
// * CreateDate    : 2013-11-25 16:35:05
// * Creater       : CodeSmith
// * LastChangeDate: 2013-11-25 16:35:05
// * LastChanger   : 2013-11-25 16:35:05
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Bom
{
    /// <summary>
    /// BomMaster
    /// </summary>
    public partial class BomMasterBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objBomMasterEntity">Entity</param>
        /// <returns>Entity</returns>
        public static BomMasterEntity GetBomMaster(BomMasterEntity objBomMasterEntity)
        {
            return DataAccess.SelectSingle<BomMasterEntity>(objBomMasterEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>m
        /// <param name="objBomMasterEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertBomMaster(BomMasterEntity objBomMasterEntity)
        {
            return DataAccess.Insert(objBomMasterEntity);
        }

        public static bool InsertBomMaster(List<BomMasterEntity> objBomMasterEntityList)
        {
            return DataAccess.Insert(objBomMasterEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objBomMasterEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateBomMaster(BomMasterEntity objBomMasterEntity)
        {
            return DataAccess.Update(objBomMasterEntity);
        }

        public static bool UpdateBomMaster(List<BomMasterEntity> objBomMasterEntityList)
        {
            return DataAccess.Update<BomMasterEntity>(objBomMasterEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objBomMasterEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteBomMaster(BomMasterEntity objBomMasterEntity)
        {
            return DataAccess.Delete(objBomMasterEntity);
        }

        public static bool DeleteBomMaster(List<BomMasterEntity> objBomMasterEntityList)
        {
            return DataAccess.Delete<BomMasterEntity>(objBomMasterEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetBomMasterAll()
        {
            return DataAccess.SelectAllDataSet<BomMasterEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<BomMasterEntity> GetBomMasterAllList()
        {
            return DataAccess.SelectAll<BomMasterEntity>();
        }
        #endregion

        public static bool AddBomMasterAndItem(BomMasterEntity bomMaster, List<BomItemEntity> bomItems)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert(bomMaster, broker);
                    DataAccess.Insert<BomItemEntity>(bomItems, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }

        }

        public static bool DelBomMaster(BomMasterEntity bm)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    var bis = DataAccess.Select<BomItemEntity>(new BomItemEntity { BomMasterId = bm.Id });
                    DataAccess.Delete(bis, broker);
                    DataAccess.Delete(bm, broker);
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static List<BomMasterEntity> GetBomMasterAllListByProductId(string productId)
        {
            return DataAccess.Select<BomMasterEntity>(new BomMasterEntity { ProductId = productId });
        }

        public static List<BomMasterEntity> GetBomMasterActiveAllListByProductId(string productId)
        {
            return DataAccess.Select<BomMasterEntity>(new BomMasterEntity { ProductId = productId, Status = Common.WfStatus.WFS30.ToString(), IsActive = 1 });
        }

        //获取所有生效的bommaster
        public static List<BomMasterEntity> GetAllActiveBomMaster()
        {
            return DataAccess.Select<BomMasterEntity>(new BomMasterEntity {  Status = Common.WfStatus.WFS30.ToString(), IsActive = 1 });
        }

        public static DataSet GetAllDsByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))//无查询条件
            {
                string sqlSource = @"select top {0} *,(select count(1) from BOM_MASTER) as cnt
from BOM_MASTER
where id not in (
    select top ({1}*({2}-1)) id from BOM_MASTER order by created_date
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"select bm.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER,p.PRODUCT_NAME,p.PRODUCT_CODE 
from ({0}) bm
left join [BPMDB].dbo.PW_User u1 on bm.CREATED_BY=u1.userNo
left join [BPMDB].dbo.PW_User u2 on bm.UPDATED_BY=u2.userNo
left join MST_PRODUCT p on bm.PRODUCT_ID=p.PRODUCT_ID";
                sql = string.Format(sql, sqlSource);

            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date
) order by created_date desc";
                string sqlSource = @"select bm.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER,p.PRODUCT_NAME,p.PRODUCT_CODE 
from BOM_MASTER bm
left join [BPMDB].dbo.PW_User u1 on bm.CREATED_BY=u1.userNo
left join [BPMDB].dbo.PW_User u2 on bm.UPDATED_BY=u2.userNo
left join MST_PRODUCT p on bm.PRODUCT_ID=p.PRODUCT_ID
WHERE 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);

        }

        public static string GetBomMaxVersion(string productId)
        {
            string sql = @"select max(ver) maxVer from BOM_MASTER where product_Id ='" + productId + "'";
            string maxVer = "1.0";
            string newMaxVer = "";
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                maxVer = ds.Tables[0].Rows[0]["maxVer"].ToString();
                if (string.IsNullOrEmpty(maxVer))
                    newMaxVer = "1.0";
                else
                    newMaxVer = (Convert.ToDecimal(maxVer) + 1).ToString("F1");
            }
            else
                newMaxVer = "1.0";

            return newMaxVer;
        }


    }


}

